
//
// commander.h
//
// Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
//

#ifndef COMMANDER_H
#define COMMANDER_H

#if defined(WIN32)
#include <tchar.h>
#else
typedef char TCHAR;
#define _T(x)      x
#define _ftprintf fprintf
#define _tprintf printf
#define _tcsrchr strrchr
#define _tcslen strlen
#define _tcscmp strcmp
#endif

/*
 * Max options that can be defined.
 */

#ifndef COMMANDER_MAX_OPTIONS
#define COMMANDER_MAX_OPTIONS 32
#endif

/*
 * Max arguments that can be passed.
 */

#ifndef COMMANDER_MAX_ARGS
#define COMMANDER_MAX_ARGS 32
#endif

/*
 * Command struct.
 */

struct command;

/*
 * Option callback.
 */

typedef int (* command_callback_t)(struct command *self, void* user_param);

/*
 * Command option.
 */

typedef struct {
  int optional_arg;
  int required_arg;
  TCHAR *argname;
  TCHAR *large;
  const TCHAR *small;
  const TCHAR *large_with_arg;
  const TCHAR *description;
  command_callback_t cb;
  void* user_param;
  int	parse_result;
} command_option_t;

/*
 * Command.
 */

typedef struct command {
  void *data;
  const TCHAR *usage;
  const TCHAR *arg;
  const TCHAR *name;
  const TCHAR *version;
  int option_count;
  command_option_t options[COMMANDER_MAX_OPTIONS];
  int argc;
  TCHAR *argv[COMMANDER_MAX_ARGS];
} command_t;

// prototypes

void
command_init(command_t *self, const TCHAR *name, const TCHAR *version);

int
command_help(command_t *self, void* param);

void
command_option(command_t *self, const TCHAR *small, const TCHAR *large, const TCHAR *desc, command_callback_t cb, void* param);

void
command_parse(command_t *self, int argc, TCHAR **argv);

void
command_deinit(command_t *self);

#endif /* COMMANDER_H */